package com.coollang.smartbra.activity;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.CountDownTimer;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.ScaleAnimation;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.coollang.smartbra.MyAutoLayoutActivity;
import com.coollang.smartbra.R;
import com.coollang.smartbra.base.MyApplication;
import com.coollang.smartbra.eventbus.CommonEvent;
import com.coollang.smartbra.utils.TranslucentSystemBarUtils;

import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;

/**
 * @author Timmy
 *         3,2,1 倒计时页面
 */
public class CountTimeActivity extends MyAutoLayoutActivity {
    @BindView(R.id.status_bar)
    View statusBar;
    @BindView(R.id.content)
    RelativeLayout content;
    @BindView(R.id.tv_count)
    TextView tvCount;
    private MediaPlayer mMediaPlayer;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_counttime);
        ButterKnife.bind(this);
        TranslucentSystemBarUtils.showChenJinZhuangTaiLan(statusBar, this, content);
    }

    @Override
    protected void onStart() {
        super.onStart();
        // 方法缩小
        final ScaleAnimation scaleAnimation =
                new ScaleAnimation(0f, 1f, 0f, 1f, ScaleAnimation.RELATIVE_TO_SELF, 0.5f, ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
        scaleAnimation.setDuration(300);
        statusBar.setBackgroundColor(getResources().getColor(R.color.progrss_bg));
        new CountDownTimer(5000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {

                int voiceID = -1;

                if (millisUntilFinished > 4000) {
                    tvCount.setText("3");
                    voiceID = R.raw.voice_count_3;
                } else {
                    if (millisUntilFinished > 3000 && millisUntilFinished < 4000) {
                        voiceID = R.raw.voice_count_2;
                        tvCount.setText("2");
                    } else if (millisUntilFinished > 2000 && millisUntilFinished < 3000) {
                        voiceID = R.raw.voice_count_1;
                        tvCount.setText("1");
                    } else if (millisUntilFinished < 2000) {
                        voiceID = R.raw.voice_count_start;
                        tvCount.setText("GO");
                    }
                }

                playVoice(voiceID);
                tvCount.startAnimation(scaleAnimation);
            }

            @Override
            public void onFinish() {
                EventBus.getDefault().post(new CommonEvent("", CommonEvent.REQUESTSUEECCD, CommonEvent.START));
                finish();
            }
        }.start();
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void onDestroy() {
        if (mMediaPlayer != null) {
            mMediaPlayer.stop();
            mMediaPlayer.release();
            mMediaPlayer = null;
        }

        super.onDestroy();

    }

    private void playVoice(int voiceID) {

        if (voiceID != 0) {
            if (mMediaPlayer != null) {
                mMediaPlayer.reset();
                mMediaPlayer.release();
                mMediaPlayer = null;
            }

            //播放音乐
            mMediaPlayer = new MediaPlayer();
            try {

                String packageName = MyApplication.getApplication().getPackageName();
                mMediaPlayer.setDataSource(CountTimeActivity.this, Uri.parse("android.resource://" + packageName + "/" + voiceID));
                mMediaPlayer.prepareAsync();//异步准备
                mMediaPlayer.setOnPreparedListener(mOnPreparedListener);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private MediaPlayer.OnPreparedListener mOnPreparedListener = new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            mp.start();
        }
    };

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return false;
    }
}
